import http
import dns.resolver

class DnstoIp:
    iplist = []
    def __init__(self,domain):
        self.domain = domain
    def showIP(self):
        try:
            A = dns.resolver.resolve(self.domain, 'A')
        except Exception as e:
            print(e)
        for i in A.response.answer:
            for j in i.items:
                self.iplist.append(j.address)
        return True
    def checkip(self,ip):
        checkurl = ip+":80"
        getcotent = ""
        #  设置超时时间
        http.client.socket.setdefaulttimeout(5)
        conn = http.client.HTTPConnection(checkurl, timeout=5)
        try:
            conn.request("GET","/",headers = {"Host":self.domain})
            r = conn.getresponse()
            getcotent = r.read(15).decode("utf-8")
        except Exception as e:
            print(ip + " " + "error:", e)
        finally:
            if getcotent == "<!DOCTYPE html>":
                print(ip+"  "+"ok")
            else:
                print(ip+"  "+"error")

if __name__ == '__main__':
    IP = input("请输入域名：")
    resover = DnstoIp(IP)
    if resover.showIP() and len(resover.iplist) > 0:
        for i in resover.iplist:
            resover.checkip(i)
        else:
            print("域名解析失败")